<?php
namespace app\admin\controller;


use \think\Validate as Validate;
class Choice extends Base{

    public function index(){

        $model = new \app\admin\model\Paper;
        $paper = $model->select();
        $this->assign('paper',$paper);
        return $this->fetch();

    }

    /**
     * 显示题目表
     * 
     * @return json;
     * @date Mon Jul  2 11:35:05 2018
     */
    public function show(){
        $post = $this->req->post();
        $rule = [
            'id'=> 'require'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new \app\admin\model\PaperChoice;
            $data = $model->where(['pap_id'=>$post['id']])->select();
            if($data){
                $this->assign('data',$data);
                $info['data'] = $this->fetch(); 
                $info['msg'] = '数据获取成功';
                echo $this->right($info);
            } else{
                $info['msg'] = '数据获取失败';
                echo $this->wrong($info);
            }
            
        } else {
            $info['msg'] = '数据获取失败';
            echo $this->wrong($info);
        }
        return;
    }

    /**
     * 某一试卷的所有选择题列表，不分页！
     * 
     * @return 
     * @date Mon Jul  2 13:06:38 2018
     */
    public function choice(){
        $get = $this->req->get();
        $rule = [
            'id'=> 'require'
        ];
        $validate = new Validate($rule);
        if($validate->check($get)){
            $model = new \app\admin\model\PaperChoice;
            $data = $model->where(['pap_id'=>$get['id']])->select();
            if($data){
                $this->assign('data',$data);
                return $this->fetch();
            } else{
                return $this->error();
            }
            
        } else {
            return $this->error();
        }
        return;
    }

    public function add(){
        $model = new \app\admin\model\Paper;
        $paper = $model->where(['pap_type'=>2])->select();
        $this->assign('paper',$paper);
        return $this->fetch();
    }
    public function special(){
        $get = $this->req->get();
        $validate = new Validate(['id'=>'require']);
        if($validate->check($get)){
            $model = new \app\admin\model\Paper;
            $paper = $model->field('pap_id,pap_name')->where(['pap_id'=>$get['id']])->find();
            if($paper){
                $this->assign('paper',$paper);
                return $this->fetch();
            }else{
                return $this->error('你要找的试卷没有找到');
            }
        }else{
            return $this->fetch();
        }
    }
    /**
     * 插入新增试题，接受方式：post
     * 
     * @return 
     * @date Sat Jun  9 20:01:09 2018
     */
    public function insert()
    {
        $post                  = $this->req->post();
        // var_dump($post);exit();
        $options = $post['options'];
        $rule = [
            'title' => 'require',
            'pid' => 'require',
            'options' => function($value,$options) {
                foreach($value as $v){
                    if(empty($v)){
                        return false;
                    }else{
                        return true;
                    }
                }
            },
        ];
        $validate = new Validate($rule);
        // $options_validate = new Validate($rule)
        if($validate->check($post)){
            $data['ch_title']      = $post['title'];
            $data['ch_options']    = serialize($post['options']);
            $data['ch_result']     = isset($post['result']) ? serialize($post['result']) : 0;
            $data['ch_createtime'] = time();
            $data['pap_id']        = $post['pid'];
            $choiceModel           = new \app\admin\model\PaperChoice;
            $result = 1;
            // $result                = $choiceModel->save($data);
            if($result){
                $info['msg'] = '添加成功';
                echo $this->right($info);
            }else{
                $info['msg'] = '添加成功';
                echo $this->wrong($info);
            }
            
        }else{
            $info['msg'] = '数据不完整';
            echo $this->wrong($info);
        }            
        return;
    }


    /**
     * 添加选择题ajax模板方法;
     * 
     * @return html/template
     * @date Sat Jun  9 20:10:15 2018
     */
    public function mob()
    {
        echo $this->fetch();
    }

    /**
     * 编辑题目；
     * 
     * @return 
     * @date Wed Jun 20 16:17:20 2018
     */
    public function edit(){
        $id = $this->req->param('id');
        $model = new \app\admin\model\PaperChoice;
        $data = $model->getchoice($id);
        // var_dump($data['ch_result']);
        // var_dump($data['ch_options']);
        // var_dump($data);
        foreach($data['ch_options'] as $key=>$value){
            $temp[$key] = [
                    'option' => $value,
                    'check'=>'',
            ];
        }
        foreach($temp as $key=>$value){
            if(isset($data['ch_result'][$key])){
                $k = ($data['ch_result'][$key]);

                $temp[$k]['check'] = 'checked';
            } 
        }
        $data['value'] = $temp;
        // var_dump($temp);
        $this->assign('data',$data);

        $model = new \app\admin\model\Paper;
        $paper = $model->field('pap_id,pap_name')->select();
        $this->assign('paper',$paper);
        return $this->fetch();
    }
    public function update(){
        $_data                 = $this->req->post();
        $chid                  = $_data['chid'];
        $data['ch_title']      = $_data['title'];
        $data['ch_options']    = serialize($_data['options']);
        $data['ch_result']     = isset($_data['result']) ? serialize($_data['result']) : 0;
        $data['ch_createtime'] = time();
        $data['pap_id']        = $_data['pid'];

        $choiceModel = new \app\admin\model\PaperChoice;
        $result = $choiceModel->where(['ch_id'=>$chid])->update($data);
        if($result){
            echo $this->alert(1,'更新成功');
        }else{
            echo $this->alert(0,'更新失败');
        }
        
    }
}
